package item18;

import org.junit.Test;

/**
 * @author Lv Jing
 * @date 2018年07月19日 23:32
 */
public class DeleteDumpNode {

    @Test
    public void test01() {
        Solution2 solution = new Solution2();
        ListNode head = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;

        head = solution.deleteDuplication(head);

        printListNode(head);
    }

    @Test
    public void test02() {
        Solution2 solution = new Solution2();
        ListNode head = new ListNode(1);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = solution.deleteDuplication(head);
        printListNode(head);
    }

    @Test
    public void test03() {
        Solution2 solution = new Solution2();
        ListNode head = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(3);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = solution.deleteDuplication(head);
        printListNode(head);
    }

    public void printListNode(ListNode head) {
        while (head != null) {
            System.out.print(head.val);
            System.out.print("|");
            head = head.next;
        }
        System.out.println();
    }
}
class Solution2 {

    /**
     * 面试题18-2：删除链表中的重复节点。
     * 思路：顺序遍历链表，当遇到一个节点，它的next的val等于它的val，那么这两个节点就是连续重复的，需要删除，让当前节点的pre指向
     * 下一节点的next，再次进行判断。需要注意的是，如果重复节点在头节点位置，那么需要注意它是没有pre节点的，这时不能让pre.next指向
     * next.next，而是指向把头结点指向next.next。
     */
    public ListNode deleteDuplication(ListNode pHead)
    {
        if (pHead == null) {
            return null;
        }

        ListNode preNode = null;
        ListNode node = pHead;
        while (node != null) {
            ListNode nextNode = node.next;
            boolean delFlag = false;

            if (nextNode != null && nextNode.val == node.val) {
                delFlag = true;
            }

            // 不需要删除的直接遍历下一节点
            if (!delFlag) {
                preNode = node;
                node = node.next;
                continue;
            }

            int val = node.val;
            ListNode delNode = node;
            // 遍历到删除节点的下一节点不等于它的值为止，也就是说它之后紧跟的节点，只要跟它的值相等都要删除
            while (delNode != null && delNode.val == val) {
                nextNode = delNode.next;
                delNode = nextNode;
            }

            if (preNode == null) {
                pHead = nextNode;
            } else {
                preNode.next = nextNode;
            }
            node = nextNode;
        }

        return pHead;
    }
}
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}